package com.gitee.huxing.framework.redis;

import cn.hutool.core.lang.Assert;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.framework.redis
 * @ClassName: RedisMessageSubscriber
 * @Author: huxing
 * @DateTime: 2021-10-26 下午4:19
 */
@Slf4j
public class RedisMessageSubscriber implements MessageListener {

    public static List<String> messageList = new ArrayList<>();

    @Override
    public void onMessage(Message message, byte[] pattern) {
        messageList.add(message.toString());
        log.info("订阅方接收到了消息==>{}", message.toString());
        // 检查消息处理器是否为空
        MessageHandler messageHandler = SpringUtil.getBean("messageHandler");
        if (messageHandler != null){
            // 处理消息内容
            messageHandler.handler(message.toString());
        }
    }
}
